package com.aerfafish.spider.neo4j.spider.service.impl;

import com.aerfafish.spider.neo4j.dao.Share;
import com.aerfafish.spider.neo4j.repository.ShareRepository;
import com.aerfafish.spider.neo4j.spider.service.ShareService;
import org.neo4j.driver.exceptions.TransientException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author aerfafish
 * @date 2021/4/21 10:06 上午
 */
@Service
public class ShareServiceImpl implements ShareService {

    private final static Logger logger = LoggerFactory.getLogger(ShareServiceImpl.class);

    @Autowired
    ShareRepository shareRepository;

    @Override
    public Share queryShareByShareCode(String shareCode) throws Exception {
        Pattern pattern = Pattern.compile("^[^0-9]*([0-9]*$)");
        Matcher matcher = pattern.matcher(shareCode);
        // 去掉前面的字母，也就是市场名字
        if (matcher.find()) {
            shareCode = matcher.group(1);
        } else {
            throw new Exception("queryShareByCode: match share code error");
        }
        List<Share> shares = new ArrayList<>();
        try {
            shares = shareRepository.queryByShareCode(shareCode);
        } catch (TransientException | InvalidDataAccessApiUsageException e) {
            try {
                shares = shareRepository.queryByShareCode(shareCode);
            } catch (TransientException | InvalidDataAccessApiUsageException e0) {
                shares = shareRepository.queryByShareCode(shareCode);
            }
        }
        if (shares.size() > 1) {
            throw new Exception("some share code is repeated");
        }
        if (shares.size() == 0) {
            return null;
        }
        // 取第一个
        return shares.iterator().next();
    }

    @Override
    public void save(Share share) throws Exception {
        save(share.getCode(), share.getMarketNum(), share.getMarket(), share.getShareName(), share.getLatestValue(), share.getIncRate());
    }

    @Override
    public void save(String code, Integer marketNum, String market, String shareName, Float latestValue, Float incRate) throws Exception {
        if (code == null || marketNum == null || market == null || shareName == null || latestValue == null || incRate == null) {
            throw new Exception("share info missing");
        }

        try {
            shareRepository.save(code, marketNum, market, shareName, latestValue, incRate);
        } catch (TransientException | InvalidDataAccessApiUsageException e) {
            try {
                shareRepository.save(code, marketNum, market, shareName, latestValue, incRate);
            } catch (TransientException | InvalidDataAccessApiUsageException e0) {
                shareRepository.save(code, marketNum, market, shareName, latestValue, incRate);
            }
        }

    }

    @Override
    public List<String> queryAllShareByCode() {
        List<String> shareCodes = shareRepository.queryAllShareByCode();
        shareCodes.removeIf(Objects::isNull);
        return shareCodes;
    }

}
